﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.Client.App;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Interface;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Control.DateTimeGrid;
using ZhimeiSoft.Framework.Client.Control;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class TodayReservationView : BaseUserControl
    {

        /// <summary>
        /// 当天调休的员工
        /// </summary>
        private DataSet dsEmployeeVacationDay = new DataSet();
        /// <summary>
        /// 当前有效的员工
        /// </summary>
        private DataSet dsEmployeeInfos = new DataSet();
        /// <summary>
        /// 系统会员
        /// </summary>
        private DataSet dsCustomInfo = new DataSet();

        /// <summary>
        ///  获取当前天所有的预约记录
        /// </summary>
        private DataSet dsReservation = new DataSet();

        /// <summary>
        /// 获取当前天所有的预约记录明细
        /// </summary>
        private DataSet dsReservationServiceItem = new DataSet();

        public TodayReservationView()
        {
            InitializeComponent();
            InitRobbinBar();
            
        }
        public TodayReservationView(CharmManagerApp _app)
            : base(_app)
        {
            InitializeComponent();
            
            initData();
            InitRobbinBar();
            todayReservation.OnGridDBClick = eventGridOnDBClick;
            customReservationTimeOut.EventDBClick = eventDBClick;
            PhoneCallCtrl phoneCtrl = PhoneCallCtrl.getPhoneCallCtrl(this);
            if (!phoneCtrl.IsStartCall)
            {
                //自动运行
                phoneCtrl.InitPort();
            }
            phoneCallControl1.initData();
            
        }

        private void InitRobbinBar()
        {
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar1);
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar2);
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar3);
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar4);
        }

        private bool initData()
        {
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryEmployeeVacationDay(DateTime.Now.ToShortDateString(), DBEntityEmoloyeeVacation.ENUM_STATE.VOA,
                out dsEmployeeVacationDay, out error))
            {
                MsgBox.MsgInfo("获取员工休假信息错误：" + error);
                return false;
            }

            if (!BussinessProxyManager.Instance.QueryEmployeeState(DBEntityEmployee.ENUM_ESTATE.F0E, out dsEmployeeInfos, out error))
            {
                MsgBox.MsgInfo("获取员工信息错误：" + error);
                return false;
            }
            //过滤可以预约美容师
            DataRow[] drIsReservation = dsEmployeeInfos.Tables[0].Select(DBEntityEmployee.FieldName.ISRESERVERATION + " = 0");
            foreach (DataRow dr in drIsReservation)
            {
                dr.Delete();
            }

            dsEmployeeInfos.AcceptChanges();

            if (!BussinessProxyManager.Instance.QueryResveration(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, string.Empty, out dsReservation, out error))
            {
                MsgBox.MsgInfo("获取预约记录错误：" + error);
                return false;
            }


            if (!BussinessProxyManager.Instance.QueryReservationEmployee(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, -1,
                string.Empty, out dsReservationServiceItem, out error))
            {
                MsgBox.MsgInfo("获取员工预约信息错误：" + error);
                return false;
            }
            //获取员工数据
            if (!BussinessProxyManager.Instance.QueryCustomState(string.Empty, out dsCustomInfo, out error))
            {
                MsgBox.MsgInfo("获取用户信息错误：" + error);
                return false;
            }
            //今天的预约绑定
            //todayReservation.BindData(dsReservation, dsEmployeeInfos.Tables[0], dsCustomInfo.Tables[0]);

            todayReservation.BindData(dsReservation, dsReservationServiceItem, dsEmployeeInfos.Tables[0], dsCustomInfo.Tables[0]);
            //预约绑定员工
            //todayReservationEmployee.DataBind(dsEmployeeInfos.Tables[0], DateTime.Now, dsReservationServiceItem);

            //获取当前的待办事项
            toDoListControl.InitControl(DateTime.Now);

            return true;
        }


        private void btnAddReservatioin_Click(object sender, EventArgs e)
        {
            frmTodayReservationCustom frmReservation = new frmTodayReservationCustom();
            if (frmReservation.ShowDialog() == DialogResult.OK)
            {
                initData();
            }
        }


        private void buttonItem2_Click(object sender, EventArgs e)
        {
            if (todayReservation.ReservationInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约记录。");
                return;
            }
            frmTodayReservationFinished frmFinished = new frmTodayReservationFinished(todayReservation.ReservationInfo);
            if (frmFinished.ShowDialog() == DialogResult.OK)
            {
                initData();
            }
        }

        

        #region 今日预约
        /// <summary>
        /// 当前预约双击
        /// </summary>
        /// <param name="drReservation"></param>
        private void eventGridOnDBClick(DataRow drReservation)
        {
            //进行预约调整
            //btnResverionEdit_Click(null, null);
            DBEntityReservation reservationInfo = todayReservation.ReservationInfo;
            if (reservationInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约记录。");
                return;
            }

            frmAdjustReservation frmReservation = new frmAdjustReservation(reservationInfo);
            if (frmReservation.ShowDialog() == DialogResult.OK)
            {
                initData();
            }
        }
        /// <summary>
        /// 调整到当前时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditReservation_Click(object sender, EventArgs e)
        {
            if (todayReservation.ReservationInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约记录。");
                return;
            }
            if (!MsgBox.MsgQue("是否将当前预约调整到当前时间？"))
            {
                return;
            }
            string error = string.Empty;
            int minute = DateTime.Now.Minute;
            int hours = DateTime.Now.Hour;
            if (minute >= 1 && minute < 15)
            {
                minute = 15;
            }
            else if (minute >= 15 && minute < 30)
            {
                minute = 30;
            }
            else if (minute >= 30 && minute < 45)
            {
                minute = 45;
            }
            else if (minute >= 45 && minute < 59)
            {
                minute = 1;
                hours = DateTime.Now.Hour + 1;
            }
            DateTime reservationTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                hours, minute, 1);
            DBEntityReservation reservation = (DBEntityReservation)todayReservation.ReservationInfo.Clone();
            reservation.RESERVATIONTIME = reservationTime;
            if (!BussinessProxyManager.Instance.AdjstReservationCurrTime(reservation, out error))
            {
                MsgBox.MsgInfo("调整错误：" + error);
                return;
            }
            MsgBox.MsgInfo("调整完毕。");
            initData();
        }
        private void btnReferch_Click(object sender, EventArgs e)
        {

            if (initData())
            {
                MsgBox.MsgInfo("数据刷新完毕。");
            }
        }
        #endregion

        #region 本月预约
        private void buttonItem6_Click(object sender, EventArgs e)
        {
            DBEntityReservation reservationInfo = reservationMonth2.ReservationInfo;
            if (reservationInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约记录。");
                return;
            }
            frmAdjustReservation frmAdjust = new frmAdjustReservation(reservationInfo);
            if (frmAdjust.ShowDialog() == DialogResult.OK)
            {
                buttonItem7_Click(sender, e);
            }
        }


        private void buttonItem7_Click(object sender, EventArgs e)
        {
            reservationMonth2.RefreshData();
            initData();
        }

        #endregion

        #region 会员预约
        private void buttonItem3_Click(object sender, EventArgs e)
        {
            DBEntityReservation reservationInfo = customReservationForMonth1.getCurrReservation;
            if (reservationInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约记录。");
                return;
            }
            frmAdjustReservation frmAdjust = new frmAdjustReservation(reservationInfo);
            if (frmAdjust.ShowDialog() == DialogResult.OK)
            {
                buttonItem4_Click(sender, e);
            }
        }

        private void buttonItem4_Click(object sender, EventArgs e)
        {
            customReservationForMonth1.refreshData();
            initData();
        }
        #endregion

        #region 预约过期
        private void tabItem4_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            //预约过期
            DataSet dsReservation_C0D = null;
            string filter = DBEntityReservation.FieldName.RSTATE + " = '" + DBEntityReservation.ENUM_RSTATE.C0D + "'";
            if (!BussinessProxyManager.Instance.QueryReservationMonth(-1, -1, filter, out dsReservation_C0D, out error))
            {
                MsgBox.MsgInfo("获取预约过期数据错误：" + error);
                return;
            }
            customReservationTimeOut.InitQueryReservation(dsReservation_C0D);
            
            initData();
        }

        public void eventDBClick(DataRow drData)
        {
            DBEntityReservation adjReservation = new DBEntityReservation(drData);
            frmAdjustReservation frmAjd = new frmAdjustReservation(adjReservation);
            if (frmAjd.ShowDialog() == DialogResult.OK)
            {
                tabItem4_Click(null, null);
            }
        }

        private void btnPage4AdjReservation_Click(object sender, EventArgs e)
        {
            DBEntityReservation reservationInfo = customReservationTimeOut.GetReservationInfo;
            if (reservationInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约记录。");
                return;
            }

            frmAdjustReservation frmAdjustReservation = new frmAdjustReservation(reservationInfo);
            frmAdjustReservation.ShowDialog();
            tabItem4_Click(sender, e);

        }

        private void buttonItem13_Click(object sender, EventArgs e)
        {
            tabItem4_Click(sender, e);
        }
        #endregion

        private void grdViewScheduleMonth_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {

        }

        #region 预约调整
        private void btnResverionEdit_Click(object sender, EventArgs e)
        {
            if (todayReservation.ReservationInfo == null)
            {
                MsgBox.MsgInfo("请选择正确的预约记录。");
                return;
            }

            frmAdjustReservation frmReservation = new frmAdjustReservation(todayReservation.ReservationInfo);
            if (frmReservation.ShowDialog() == DialogResult.OK)
            {
                initData();
            }

        }

        
        #endregion

        private void textBoxItem1_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void txtCustomSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.CompareTo('\r') == 0)
            {
                CustomSearch customSear = new CustomSearch();
                customSear.SearchCustomInfo(txtCustomSearch.Text.Trim());
                txtCustomSearch.Text = string.Empty;
            }
        }

        
    }
}
